import moment from 'moment';

const filterFunc = (q, fields) => {
  if(fields == undefined || fields.length == 0){
    return q;
  }
  else
  return '((' + q.split(' ').map(function (qa) {
    return fields.map(function (field) {
      return "contains(" + field + ",'" + qa + "')";
    }).join(' or ');
  }).join(') and (') + '))';
}
const filterFuncTwo = (fields,dataObj,contains) => {
  return fields.map(function (field, fieldIndex) {
      if(field == 'time'){
        if(dataObj.time[0].toString() != "Invalid Date"){
          return `(timestamp ge '${moment(dataObj.time[0]).utc().format()}' and timestamp le '${moment(dataObj.time[1]).utc().format()}')`
        }
      }else{
        if(dataObj[field]){
          if(contains){
            return "contains(" + field + ",'" + dataObj[field] + "')";
          }else{
            return "(" + field + " eq '" + dataObj[field] + "')";
          } 
        }
      }
  }).filter( s => s && s.trim() ).join(' and ');
}
const tableParams = (table, fields) => {
  return {
    $limit: 10,
    $skip: 0,
    
  }
}

export { filterFunc, filterFuncTwo };
